using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CImageMatchedButDifferent : CBaseDynamicM2M, IComparable<CImageMatchedButDifferent>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CImageMatchedButDifferent(CImageMatchedButDifferent original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _mD5 = original.MD5;
            _matchingMd5 = original.MatchingMd5;
            _score = original.Score;
        }

        //Protected (Datareader/Dataset)
        protected CImageMatchedButDifferent(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CImageMatchedButDifferent(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _mD5 = Guid.Empty;
            _matchingMd5 = Guid.Empty;
            _score = int.MinValue;
        }
        #endregion

        #region Members
        protected Guid _mD5;
        protected Guid _matchingMd5;
        protected int _score;
        #endregion

        #region Properties - Column Values
        //Primary Key Columns
        public Guid MD5    {   get { return _mD5; } set { _mD5 = value; }    }
        public Guid MatchingMd5    {   get { return _matchingMd5; } set { _matchingMd5 = value; }    }

        //Table Columns (Read/Write)
        public int Score    {   get { return _score; } set { _score = value; }    }

        //View Columns (ReadOnly)

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "ImageMatchedButDifferent";
        public const string VIEW_NAME       = "";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "MD5";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CImageMatchedButDifferent other) {   return this.MD5.CompareTo(other.MD5) ;  }

        //Primary Key Information
        protected override string PrimaryKeyName    {   get { return "MD5";    } }
        protected override string SecondaryKeyName  {   get { return "MatchingMd5";  } }        protected override object PrimaryKeyValue 
        {
              get { return _mD5; }
              set { _mD5 = (Guid)value; }
        }
        protected override object SecondaryKeyValue 
        {
              get { return _matchingMd5; }
              set { _matchingMd5 = (Guid)value; }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)        {   return new CImageMatchedButDifferent(this.DataSrc, row);     }
        protected override CBase MakeFrom(IDataReader dr)     {   return new CImageMatchedButDifferent(this.DataSrc, dr);      }

        //Factory Methods - List
        protected override IList MakeList()                   {   return new CImageMatchedButDifferentList();                  }
        protected override IList MakeList(int capacity)       {   return new CImageMatchedButDifferentList(capacity);          }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _mD5 = CAdoData.GetGuid(dr, "MD5");
            _matchingMd5 = CAdoData.GetGuid(dr, "MatchingMd5");
            _score = CAdoData.GetInt(dr, "Score");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _mD5 = CAdoData.GetGuid(dr, "MD5");
            _matchingMd5 = CAdoData.GetGuid(dr, "MatchingMd5");
            _score = CAdoData.GetInt(dr, "Score");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("MD5", CAdoData.NullVal(_mD5));
            data.Add("MatchingMd5", CAdoData.NullVal(_matchingMd5));
            data.Add("Score", CAdoData.NullVal(_score));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CImageMatchedButDifferentList SelectAll()                                                          {    return (CImageMatchedButDifferentList)base.SelectAll();                              }
        public new CImageMatchedButDifferentList SelectAll(string orderBy)                                            {    return (CImageMatchedButDifferentList)base.SelectAll(orderBy);                       }
        public new CImageMatchedButDifferentList SelectWhere(string colName, ESign sign, object colValue)             {    return (CImageMatchedButDifferentList)base.SelectWhere(colName, sign, colValue);     }
        public new CImageMatchedButDifferentList SelectWhere(CCriteria where)                                         {    return (CImageMatchedButDifferentList)base.SelectWhere(where);                       }
        public new CImageMatchedButDifferentList SelectWhere(CCriteriaList where)                                     {    return (CImageMatchedButDifferentList)base.SelectWhere(where);                       }
        public new CImageMatchedButDifferentList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CImageMatchedButDifferentList)base.SelectWhere(where, tableOrJoin);          }
        public new CImageMatchedButDifferentList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CImageMatchedButDifferentList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CImageMatchedButDifferentList SelectWhere(string unsafeWhereClause)                                {    return (CImageMatchedButDifferentList)base.SelectWhere(unsafeWhereClause);           }
        public     CImageMatchedButDifferentList SelectById(Guid mD5, Guid matchingMd5)                              {    return (CImageMatchedButDifferentList)base.SelectById(mD5, matchingMd5);       }
        public     CImageMatchedButDifferentList SelectByIds(List<Guid> ids)                        {    return (CImageMatchedButDifferentList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CImageMatchedButDifferentList SelectAll(  CPagingInfo pi)                                              {    return (CImageMatchedButDifferentList)base.SelectAll(  pi);                              }
        public new CImageMatchedButDifferentList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CImageMatchedButDifferentList)base.SelectWhere(pi, name, sign, value);           }
        public new CImageMatchedButDifferentList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CImageMatchedButDifferentList)base.SelectWhere(pi, criteria);                    }
        public new CImageMatchedButDifferentList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CImageMatchedButDifferentList)base.SelectWhere(pi, criteria);                    }
        public new CImageMatchedButDifferentList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CImageMatchedButDifferentList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CImageMatchedButDifferentList SelectByIds(CPagingInfo pi, List<Guid> ids)            {    return (CImageMatchedButDifferentList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CImageMatchedButDifferentList SelectAll(                                                                                           IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectAll(tx);                                                     }
        public new CImageMatchedButDifferentList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectAll(orderBy,                                         tx);    }
        public new CImageMatchedButDifferentList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CImageMatchedButDifferentList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CImageMatchedButDifferentList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectWhere(               criteria,                       tx);    }
        public new CImageMatchedButDifferentList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectWhere(               criteria,                       tx);    }
        public new CImageMatchedButDifferentList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CImageMatchedButDifferentList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CImageMatchedButDifferentList SelectById(Guid mD5, Guid matchingMd5,                                                              IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectById(mD5, matchingMd5,                         tx);    }
        public     CImageMatchedButDifferentList SelectByIds(List<Guid> ids,                                                        IDbTransaction tx)  {    return (CImageMatchedButDifferentList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CImageMatchedButDifferentList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CImageMatchedButDifferentList)base.MakeList(storedProcName,             txOrNull);  }
        public new CImageMatchedButDifferentList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CImageMatchedButDifferentList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CImageMatchedButDifferentList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CImageMatchedButDifferentList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CImageMatchedButDifferentList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CImageMatchedButDifferentList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CImageMatchedButDifferentList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CImageMatchedButDifferentList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CImageMatchedButDifferentList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CImageMatchedButDifferentList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CImageMatchedButDifferentList MakeList(DataSet              ds) { return (CImageMatchedButDifferentList)base.MakeList(ds);        }
        protected new CImageMatchedButDifferentList MakeList(DataTable            dt) { return (CImageMatchedButDifferentList)base.MakeList(dt);        }
        protected new CImageMatchedButDifferentList MakeList(DataRowCollection  rows) { return (CImageMatchedButDifferentList)base.MakeList(rows);      }
        protected new CImageMatchedButDifferentList MakeList(IDataReader          dr) { return (CImageMatchedButDifferentList)base.MakeList(dr);        }
        protected new CImageMatchedButDifferentList MakeList(object           drOrDs) { return (CImageMatchedButDifferentList)base.MakeList(drOrDs);    }        
        protected new CImageMatchedButDifferentList MakeList(byte[]             gzip) { return (CImageMatchedButDifferentList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CImageMatchedButDifferentList SelectByMD5(Guid mD5)    {    return SelectWhere(new CCriteriaList("MD5", mD5));    }
        public CImageMatchedButDifferentList SelectByMatchingMd5(Guid matchingMd5)    {    return SelectWhere(new CCriteriaList("MatchingMd5", matchingMd5));    }

        //Paged
        public CImageMatchedButDifferentList SelectByMD5(CPagingInfo pi, Guid mD5)    {    return SelectWhere(pi, new CCriteriaList("MD5", mD5));    }
        public CImageMatchedButDifferentList SelectByMatchingMd5(CPagingInfo pi, Guid matchingMd5)    {    return SelectWhere(pi, new CCriteriaList("MatchingMd5", matchingMd5));    }

        //Count
        public int SelectCountByMD5(Guid mD5)   {   return SelectCount(new CCriteriaList("MD5", mD5));     }
        public int SelectCountByMatchingMd5(Guid matchingMd5)   {   return SelectCount(new CCriteriaList("MatchingMd5", matchingMd5));     }

        //Transactional
        public CImageMatchedButDifferentList SelectByMD5(Guid mD5, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("MD5", mD5), tx);    }
        public CImageMatchedButDifferentList SelectByMatchingMd5(Guid matchingMd5, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("MatchingMd5", matchingMd5), tx);    }
        #endregion

        #region Many-To-Many Helper Functions
        //Insert/Delete - NonTransactional
        public static void InsertPair(Guid mD5, Guid matchingMd5) {        InsertPair(mD5, matchingMd5, null); }
        public static int  DeletePair(Guid mD5, Guid matchingMd5) { return DeletePair(mD5, matchingMd5, null); }
        public static bool ExistsPair(Guid mD5, Guid matchingMd5) { return ExistsPair(mD5, matchingMd5, null); }
        
        //Insert/Delete - Transactional
        public static void InsertPair(Guid mD5, Guid matchingMd5, IDbTransaction txOrNull) {        InsertPair(mD5, matchingMd5, txOrNull, CDataSrc.Default); }
        public static int  DeletePair(Guid mD5, Guid matchingMd5, IDbTransaction txOrNull) { return DeletePair(mD5, matchingMd5, txOrNull, CDataSrc.Default); }
        public static bool ExistsPair(Guid mD5, Guid matchingMd5, IDbTransaction txOrNull) { return ExistsPair(mD5, matchingMd5, txOrNull, CDataSrc.Default); }

        //Insert/Delete
        public static void InsertPair(Guid mD5, Guid matchingMd5, IDbTransaction txOrNull, CDataSrc dataSrc)
        {
            CImageMatchedButDifferent pair = new CImageMatchedButDifferent(dataSrc);
            pair.MD5 = mD5;
            pair.MatchingMd5 = matchingMd5;
            pair.Save(txOrNull);
        }
        public static int DeletePair(Guid mD5, Guid matchingMd5, IDbTransaction txOrNull, CDataSrc dataSrc)
        {
            CCriteriaList where = new CCriteriaList();
            where.Add("MD5", mD5);
            where.Add("MatchingMd5", matchingMd5);

            //Faster, but not audited
            //return new CImageMatchedButDifferent(dataSrc).DeleteWhere(where, txOrNull, true);
            
            //Slower, but audit-trail and other delete logic is preserved e.g. cascade deletes
            CImageMatchedButDifferentList list = new CImageMatchedButDifferent(dataSrc).SelectWhere(where, txOrNull);
            list.DeleteAll(txOrNull);
            return list.Count;
        }
        public static bool ExistsPair(Guid mD5, Guid matchingMd5, IDbTransaction txOrNull, CDataSrc dataSrc)
        {
            return new CImageMatchedButDifferent(dataSrc).SelectById(mD5, matchingMd5, txOrNull).Count > 0;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "MD5", this.MD5);
            Store(w, "MatchingMd5", this.MatchingMd5);
            Store(w, "Score", this.Score);
        }
        #endregion

    }
}
